#include<vector>
using namespace std;
class Solution {
public:
    int maxProduct(vector<int>& nums) {
        int n = nums.size();
        vector<int> f(n), g(n);
        f[0] = g[0] = nums[0];
        for (int i = 1; i < n; i++) {
            if (nums[i] > 0)
                f[i] = max(nums[i], f[i - 1] * nums[i]);
            else
                f[i] = max(nums[i], g[i - 1] * nums[i]);
            if (nums[i] > 0)
                g[i] = min(nums[i], g[i - 1] * nums[i]);
            else
                g[i] = min(nums[i], f[i - 1] * nums[i]);
        }
        return *max_element(f.begin(), f.end());
    }
};